Encapsulation is defined as the collection of data into a single entity. It is the mechanism that connects the data the code manipulates and the code itself. Encapsulation can also be viewed as a barrier that prevents code from the outside of the barrier from accessing the data.
Encapsulation technically refers to the fact that a class's variables or data are kept secret from all other classes and are only available through member functions of the class in which they are specified.
As with encapsulation, the data in a class is protected from other classes using the data hiding concept, which is accomplished by making the members or methods of a class private. The class is then made available to the user or the public without revealing any information about its internal workings.
The data in a class is hidden from other classes, similar to encapsulation, using the data hiding concept, which is accomplished by making the members or methods of a class private, and the class is exposed to the user or the public without disclosing any implementation-specific information using the abstraction concept, so it is also referred to as a combination of data-hiding and abstraction.
Encapsulation is achieved by writing public methods in the class to set and retrieve the values of variables and by making all of the class's variables private.
With the setter and getter methods, encapsulation is better defined.
Advantages of encapsulation
Data Hiding: Data Hiding is a method of limiting our data members' access by concealing the implementation specifics. Data hiding is also possible with encapsulation. The user won't be aware of how the class is internally implemented. The user won't be able to see how the class is keeping variables' values in order. The user will only be aware that variables are initialized with the given value and that we are passing the values to a setter method.
Flexibility Depending on our needs, we can make the class's variables read-only or write-only. If we want to make the variables read-only, we must remove the setter methods from the program, such as setEmpName() and setEmpId(), or if we want to make the variables write-only, we must remove the get methods, such as getEmpName() and getEmpId().
Reusability: Additionally, encapsulation makes it easier to reuse code and adapt to changing needs.
Easy to taste : For unit testing, encapsulated code is simple to test.
Example of Encapsulation in Java
// Java Encapsulation example
class Encapsulation{
private String Name; // Private is using to achive Encapsulationpublic String getName() { // getter methodreturn Name;
}
publicvoid setName(String Name){ // setter methodthis.Name = Name;
}
}
class Main {
publicstaticvoid main(String[] args){
Encapsulation encapsulation = new Encapsulation();
encapsulation.setName("Mohan");
System.out.println("The Name of the person is : " +encapsulation.getName());
}
}
Output:
Mohan
Read Only class in Java
Java Read Only class
class Encapsulation{
private String Name="Alok"; // Private is using to achive Encapsulationpublic String getName() { // getter methodreturn Name;
}
}
Now, you can't change the value of the Name data member which is "Alok"
Encapsulation encapsulation = new Encapsulation();
encapsulation.setName("Mohan"); //C.T Error
Write Only class in Java
Java Write Only class
class Encapsulation{
private String Name; // Private is using to achive Encapsulationpublicvoid setName(String Name){ // setter methodthis.Name = Name;
}
}
Now, you can't get the value of the Name data member you can only change
Encapsulation encapsulation = new Encapsulation();
System.out.println(encapsulation.getName()); //C.T Error
Example of Encapsulation in Java
class Encapsulation{
private String EmpName;
privateint EmpId;
public String getEmpName() {
return EmpName;
}
publicvoid setEmpName(String empName) {
EmpName = empName;
}
publicint getEmpId() {
return EmpId;
}
publicvoid setEmpId(int i) {
EmpId = i;
}
}
class Main {
publicstaticvoid main(String[] args){
Encapsulation encapsulation = new Encapsulation();
encapsulation.setEmpName("Mohan");
encapsulation.setEmpId(421658);
System.out.println("The Name of the Emp is : " +encapsulation.getEmpName()+"
& EmpID is : "+encapsulation.getEmpId());
}
}
Output:
The Name of the Emp is : Mohan & EmpID is : 421658
Post your comment